﻿using Dapr.Client;
using Messhall.Application.Dto.Users.Responses;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Messhall.Application.Dto;

namespace Messhall.Service.Proxy
{
    public class RemoteCallProxy<T> : DispatchProxy
    {
        private Type _type;

        private const bool _replacementMethod = true;
        private const string _replacementMethodString = "Async";

        public RemoteCallProxy()
        {
            _type = typeof(T);
        }

        protected override object Invoke(MethodInfo targetMethod, object[] args)
        {
            var methodDeclaringType = targetMethod.DeclaringType;


            using var daprClient = new DaprClientBuilder().Build();
            var result = daprClient.InvokeMethodAsync<PagingResponseBase<UserPagingResponse>>(HttpMethod.Get, "messhall-permission-api", "/api/Users/paging");

            Task.WaitAll(result);

            var pagingResponseBase = result.Result;


            var resultValue = new object();

            var invoke = targetMethod.Invoke(resultValue, args);


            var customAttributes = targetMethod.GetCustomAttributes();

            HttpMethod method = GetHttpMethod(targetMethod.CustomAttributes); 


            var targetMethodName = targetMethod.Name;
            var targetMethodDeclaringType = methodDeclaringType;
            var declaringTypeAssembly = methodDeclaringType.Assembly;
            var targetMethodReturnTypeCustomAttributes = targetMethod.ReturnTypeCustomAttributes;
            var targetMethodCustomAttributes = targetMethod.CustomAttributes;

            Console.WriteLine("调用了");

            return "我拦截到了";
        }


        private static HttpMethod GetHttpMethod(IEnumerable<CustomAttributeData> attributes)
        {
            foreach (var item in attributes)
            {
                if (item.AttributeType.Name == "HttpGetAttribute")
                {
                    return HttpMethod.Get;
                }
                else if (item.AttributeType.Name == "HttpDeleteAttribute")
                {
                    return HttpMethod.Delete;
                }
                else if (item.AttributeType.Name == "HttpHeadAttribute")
                {
                    return HttpMethod.Head;
                }
                else if (item.AttributeType.Name == "HttpOptionsAttribute")
                {
                    return HttpMethod.Options;
                }
                else if (item.AttributeType.Name == "HttpPatchAttribute")
                {
                    return HttpMethod.Patch;
                }
                else if (item.AttributeType.Name == "HttpPostAttribute")
                {
                    return HttpMethod.Post;
                }
                else if (item.AttributeType.Name == "HttpPutAttribute")
                {
                    return HttpMethod.Put;
                }
            }

            throw new Exception("需要在接口上标记Http请求类型");
        }

        //private readonly static string DaprServicePath = "service-dapr.json";
        //private static readonly object _lock = new object();

        //private static List<DaprApiServiceModel> DaprApiServices = null;

        //public static void Create<T>()
        //{
        //    if (DaprApiServices == null)
        //    {
        //        lock (_lock)
        //        {
        //            if (DaprApiServices == null)
        //                DaprApiServices = LoadJsonFile(DaprServicePath);
        //        }
        //    }
        //    var type = typeof(T);
        //    var typeNamespaces = typeof(T).Namespace;



        //    string sss = ";";
        //}

        ///// <summary>
        ///// 解析Json文件
        ///// </summary>
        ///// <param name="FilePath">文件路径</param>
        ///// <returns></returns>
        //public static List<DaprApiServiceModel> LoadJsonFile(string FilePath)
        //{
        //    try
        //    {
        //        StreamReader streamReader = new StreamReader(FilePath, Encoding.Default);
        //        var json = streamReader.ReadToEnd();
        //        streamReader.Close();

        //        return JsonConvert.DeserializeObject<List<DaprApiServiceModel>>(json);

        //    }
        //    catch { }

        //    return null;
        //}
    }
}
